package net.lifove.android.mic;

import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.os.Bundle;

public class LifoveMicActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		start();

	}

	boolean isRecording=true;

	void start(){
		android.os.Process.setThreadPriority
		(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);

		int frequency = 11204;
		int buffersize = AudioRecord.getMinBufferSize(frequency,
				AudioFormat.CHANNEL_CONFIGURATION_MONO,
				AudioFormat.ENCODING_PCM_16BIT) + 1024;

		AudioRecord arec = new 
				AudioRecord(MediaRecorder.AudioSource.MIC,
						frequency,
						AudioFormat.CHANNEL_CONFIGURATION_MONO,
						AudioFormat.ENCODING_PCM_16BIT,
						buffersize);


		AudioTrack atrack = new AudioTrack(AudioManager.STREAM_MUSIC,
				frequency,
				AudioFormat.CHANNEL_CONFIGURATION_MONO,
				AudioFormat.ENCODING_PCM_16BIT,
				buffersize,
				AudioTrack.MODE_STREAM);

		atrack.setPlaybackRate(frequency);

		byte[] buffer = new byte[buffersize];
		//try{
		arec.startRecording();
		atrack.play();

		while(isRecording) {
			arec.read(buffer, 0, buffersize);
			atrack.write(buffer, 0,buffersize);
		}

		arec.stop();
		atrack.stop();
		
		arec.release();
		atrack.release();
		
		/*}catch(Exception e){
			e.printStackTrace();
		}*/

	}

	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		isRecording=true;
	}


	@Override
	protected void onStop() {
		super.onStop();
		isRecording=false;
	}
	


}